﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Coslen.Framework.Helpers
{
    public static class ActionLinkExtensions
    {
        public static MvcHtmlString ImageActionLink(
            this HtmlHelper helper,
            string imageUrl,
            string altText,
            string actionName,
            string controllerName,
            object routeValues,
            object linkHtmlAttributes,
            object imgHtmlAttributes)
        {
            return ImageActionLink(helper, imageUrl, string.Empty, altText, actionName, controllerName, routeValues,
                                   linkHtmlAttributes, imgHtmlAttributes);
        }

        public static MvcHtmlString ImageActionLink(
            this HtmlHelper helper,
            string imageUrl,
            string linkText,
            string altText,
            string actionName,
            string controllerName,
            object routeValues,
            object linkHtmlAttributes,
            object imgHtmlAttributes)
        {
            var linkAttributes = AnonymousObjectToKeyValue(linkHtmlAttributes);
            var imgAttributes = AnonymousObjectToKeyValue(imgHtmlAttributes);
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", imageUrl);
            imgBuilder.MergeAttribute("alt", altText);
            
            imgBuilder.MergeAttributes(imgAttributes, true);
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
            var linkBuilder = new TagBuilder("a");
            linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
            linkBuilder.MergeAttributes(linkAttributes, true);
            linkBuilder.MergeAttribute("title", altText);
            var text = linkBuilder.ToString(TagRenderMode.StartTag);
            text += imgBuilder.ToString(TagRenderMode.SelfClosing);
            text += linkText;
            text += linkBuilder.ToString(TagRenderMode.EndTag);
            return MvcHtmlString.Create(text);
        }

        private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject)
        {
            var dictionary = new Dictionary<string, object>();
            if (anonymousObject != null)
            {
                foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
                {
                    dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
                }
            }
            return dictionary;
        }

        public static MvcHtmlString SaveOrCancelSet(
            this HtmlHelper helper,
            string cancelUrl
        )
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);

            // Save Button

            var imgBuilder_Save = new TagBuilder("img");
            imgBuilder_Save.Attributes.Add("src", urlHelper.Content("~/Content/Images/yes.png"));
            imgBuilder_Save.Attributes.Add("alt", "Save");

            var saveButtonBuilder = new TagBuilder("button");
            saveButtonBuilder.Attributes.Add("class", "actionButton");
            saveButtonBuilder.Attributes.Add("type", "submit");
            saveButtonBuilder.Attributes.Add("value", "Save");

            var saveText = saveButtonBuilder.ToString(TagRenderMode.StartTag);
            saveText += imgBuilder_Save.ToString(TagRenderMode.SelfClosing);
            saveText += "Save";
            saveText += saveButtonBuilder.ToString(TagRenderMode.EndTag);

            // Cancel Action Link

            var imgBuilder_Cancel = new TagBuilder("img");
            imgBuilder_Cancel.Attributes.Add("src", urlHelper.Content("~/Content/Images/No.png"));
            imgBuilder_Cancel.Attributes.Add("alt", "Cancel");

            var cancelLinkBuilder = new TagBuilder("a");
            cancelLinkBuilder.MergeAttribute("href", cancelUrl);
            cancelLinkBuilder.Attributes.Add("class", "actionLink");

            var cancelText = cancelLinkBuilder.ToString(TagRenderMode.StartTag);
            cancelText += imgBuilder_Cancel.ToString(TagRenderMode.SelfClosing);
            cancelText += "Cancel";
            cancelText += cancelLinkBuilder.ToString(TagRenderMode.EndTag);
            
            // Put together
            var text = saveText.ToString() + "|" + cancelText.ToString();

            return MvcHtmlString.Create(text);
        }

        public static MvcHtmlString DeleteOrCancelSet(
            this HtmlHelper helper,
            string cancelUrl
        )
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);

            // Save Button

            var imgBuilder_Delete = new TagBuilder("img");
            imgBuilder_Delete.Attributes.Add("src", urlHelper.Content("~/Content/Images/yes.png"));
            imgBuilder_Delete.Attributes.Add("alt", "Delete");

            var deleteButtonBuilder = new TagBuilder("button");
            deleteButtonBuilder.Attributes.Add("class", "actionButton");
            deleteButtonBuilder.Attributes.Add("type", "submit");
            deleteButtonBuilder.Attributes.Add("value", "Delete");

            var deleteText = deleteButtonBuilder.ToString(TagRenderMode.StartTag);
            deleteText += imgBuilder_Delete.ToString(TagRenderMode.SelfClosing);
            deleteText += "Delete";
            deleteText += deleteButtonBuilder.ToString(TagRenderMode.EndTag);

            // Cancel Action Link

            var imgBuilder_Cancel = new TagBuilder("img");
            imgBuilder_Cancel.Attributes.Add("src", urlHelper.Content("~/Content/Images/No.png"));
            imgBuilder_Cancel.Attributes.Add("alt", "Cancel");

            var cancelLinkBuilder = new TagBuilder("a");
            cancelLinkBuilder.MergeAttribute("href", cancelUrl);
            cancelLinkBuilder.Attributes.Add("class", "actionLink");

            var cancelText = cancelLinkBuilder.ToString(TagRenderMode.StartTag);
            cancelText += imgBuilder_Cancel.ToString(TagRenderMode.SelfClosing);
            cancelText += "Cancel";
            cancelText += cancelLinkBuilder.ToString(TagRenderMode.EndTag);

            // Put together
            var text = deleteText.ToString() + "|" + cancelText.ToString();

            return MvcHtmlString.Create(text);
        }

    }
}
